# -*- coding: utf-8 -*-
# 默认当图片宽度大于高度时，将图片批量处理成高度*高度的最大整数倍的大小
# 例如，将1294*128的图片剪裁成1280*128的大小的图片
import os,sys
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import cv2

os.environ['CUDA_VISIBLE_DEVICEES']='2'
folder = r'/DataSet/胃镜切片/防城港中医院胃镜切片/'
path = os.listdir(folder)
#print(os.listdir(r'/Users/hjy/Desktop/blues'))

for each_bmp in path: #遍历，进行批量转换
    first_name, second_name = os.path.splitext(each_bmp)
    each_bmp = os.path.join(folder, each_bmp)
    # print(each_bmp)
    # src = '/Users/hjy/Desktop/blues.00000.bmp'
    img = Image.open(each_bmp)
    width, height = img.size # 获取图片尺寸
    print(width, height, int(width / height),width % height)
    #box = (0, 0, height * int(width / height), height) 
    box = (992,96,4448,3552)
    # 切割图像操作
    region = img.crop(box)
    #region.show()
    im = np.array(region)
    savepath = "/DataSet/胃镜切片/ndpi 3456*3456/"
       # region.save(savepath)
    #cv2.imwrite(savepath+str(each_bmp),im)
#cv2.waitKey(0)
    region.save(savepath+first_name+'.bmp') # 保存图片
